package com.sinosoft.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.base.BaseFormController;
import com.sinosoft.base.Constants;
import com.sinosoft.content.Content;
import com.sinosoft.content.ContentEn;
import com.sinosoft.content.ContentManager;
import com.sinosoft.content.ContentView;
import com.sinosoft.content.commen.ContentCommon;
import com.sinosoft.report.Item;
import com.sinosoft.report.ReportComments;
import com.sinosoft.util.CheckEmpty;
import com.sinosoft.util.DisplayGetPage;
import com.sinosoft.util.PropertiesUtil;

@Controller
public class ContentController extends BaseFormController {

	private ContentManager conMan;

	@Autowired
	public void setConMan(ContentManager conMan) {
		this.conMan = conMan;
	}

	@RequestMapping("/content/queryaddcontent.html")
	public String queryAddContent(HttpServletRequest request,
			HttpServletResponse response) {
		return "content/addcontent";
	}

	/**
	 * 新增文章
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/content/addcontent.html")
	public String addContent(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		ContentView convo = null;
		String language = request.getParameter("language");
		String conType = request.getParameter("type");
		String title = request.getParameter("title");
		String isrelease = request.getParameter("isrelease");
		String content = request.getParameter("content");
		String contentid = request.getParameter("contentid");
		String oldlanguage = request.getParameter("languageoldid");

		// 判断是修改还是新增信息
		if (!CheckEmpty.isEmpty(contentid) && !CheckEmpty.isEmpty(oldlanguage)) {
			convo = conMan.getContentById(Long.parseLong(contentid),
					oldlanguage);
			if (null != convo) {
				convo.setLanguage(language);
				if (!CheckEmpty.isEmpty(conType)) {
					convo.setContentType(Long.parseLong(conType));
				}
				if (!CheckEmpty.isEmpty(isrelease)) {
					convo.setIsRelease(Long.parseLong(isrelease));
				}
				convo.setContentTitle(title);
				convo.setContent(content);
			}
		} else {
			convo = new ContentView();
			convo.setContent(content);
			convo.setContentTime(new Date());
			convo.setContentTitle(title);
			if (!CheckEmpty.isEmpty(conType)) {
				convo.setContentType(Long.parseLong(conType));
			}
			if (!CheckEmpty.isEmpty(isrelease)) {
				convo.setIsRelease(Long.parseLong(isrelease));
			}
		}

		if (!CheckEmpty.isEmpty(oldlanguage)) {
			// 如果修改语言类型 删除原来语言表中的信息
			if (!language.equals(oldlanguage)) {
				// 新增或修改信息
				convo.setContentId(null);
				convo = conMan.saveContent(convo, language);
				conMan.removeContentEn(Long.parseLong(contentid), oldlanguage);
			} else {
				// 新增或修改信息
				convo = conMan.saveContent(convo, language);
			}
		} else {
			// 新增或修改信息
			convo = conMan.saveContent(convo, language);
		}
		String pagename = request.getParameter("pagename");
		String page = request.getParameter("thispage");
		if (!CheckEmpty.isEmpty(pagename) && !CheckEmpty.isEmpty(page)) {
			return "redirect:/content/querycontent.html?returntype=false&"
					+ pagename + "=" + page;
		} else {
			return "redirect:/content/querycontent.html";
		}
	}

	/**
	 * html 过滤
	 * 
	 * @param str
	 * @return
	 */
	private String htmlspecialchars(String str) {
		if (!CheckEmpty.isEmpty(str)) {
			str = str.replaceAll("&", "&amp;");
			str = str.replaceAll("<", "&lt;");
			str = str.replaceAll(">", "&gt;");
			str = str.replaceAll("\"", "&quot;");
		}
		return str;
	}

	/***
	 * 根据条件查询内容列表
	 * 
	 * @param view
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/content/querycontent.html")
	public ModelAndView queryContent(ContentView view, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		// 获取session
		HttpSession session = request.getSession(true);
		// 判断是否是管理页面跳转过来
		String returntype = request.getParameter("returntype");
		String page = null;
		if (!CheckEmpty.isEmpty(returntype)) {
			if (returntype.equals("false")) {
				view = (ContentView) session.getAttribute("ContentView");
			}
		}
		if (null != view) {
			session.setAttribute("ContentView", view);
		}
		// 判断页数
		page = DisplayGetPage.getPageParamName("contents", request);// 第几页
		if (page == null) {
			view.setBegin(0);
			view.setPageSize(Constants.pagesize);
		} else {
			view.setBegin((Integer.parseInt(page) - 1) * Constants.pagesize);
			view.setPageSize(Constants.pagesize);
		}

		// 获取display名称
		String pagename = DisplayGetPage.getPageName("contents", request);
		model.addAttribute("pagename", pagename);
		// 判断要查询内容的语言
		List list = null;
		if (CheckEmpty.isEmpty(view.getLanguage())) {
			String language = Constants.TABLE_TYPE_CONTENT_ZH;
			view.setLanguage(language);
		}
		
		// 查询结果
		list = conMan.getContentByType(view, view.getLanguage());
		int countsize = conMan.getContentNum(view);
		List<ContentView> listvo = null;
		if (null != list && list.size() > 0) {
			listvo = new ArrayList<ContentView>();
			for (int i = 0; i < list.size(); i++) {
				Content con = null;
				ContentEn conen = null;
				ContentView convo = null;
				if (view.getLanguage().equals(Constants.TABLE_TYPE_CONTENT_EN)) {
					conen = (ContentEn) list.get(i);
				} else {
					con = (Content) list.get(i);
				}
				convo = ContentCommon.toVo(con, conen);
				for (int j = 0; j < PropertiesUtil.getPropertiesValue(
						"content.type.list", request).length; j++) {
					if (convo.getContentType().toString()
							.equals(
									PropertiesUtil.getPropertiesValue(
											"content.type.search.list.no",
											request)[j + 1])) {
						convo.setStrContentType(PropertiesUtil
								.getPropertiesValue("content.type.list",
										request)[j]);
					}
				}

				listvo.add(convo);
			}
		}
		// 内容类型
		String[] contenttype = PropertiesUtil.getPropertiesValue("content.type.search.list",
				request);
		List<Item> contenttypelist = ReportComments.getList(contenttype);
		model.addAttribute("contenttypelist", contenttypelist);
		// 是否发布
		String[] isrelease = PropertiesUtil.getPropertiesValue("content.isrelease.list",
				request);
		List<Item> isreleaselist = ReportComments.getList(isrelease);
		model.addAttribute("isreleaselist", isreleaselist);
		// 语言类型
		String[] langagetype = PropertiesUtil.getPropertiesValue("content.language.list",
				request);
		List<Item> langagetypelist = ReportComments.getList(langagetype);
		model.addAttribute("langagetypelist", langagetypelist);

		model.addAttribute("listresult", true);
		model.addAttribute("contentsid", view);
		model.addAttribute("resultSize", countsize);
		model.addAttribute("pagesize", Constants.pagesize);
		double pagesize = Constants.pagesize;
		model.addAttribute("displayallpage", Math.ceil(countsize / pagesize));
		model.addAttribute("thispage", Integer.parseInt(page));
		return new ModelAndView("content/contentlist", "contentlist", listvo);
	}

	/**
	 * 首页内容查询列表
	 * 
	 * @param view
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 * 
	 * 点击首页  更多》
	 */
	@RequestMapping("/content/loginquerycontent.html")
	public ModelAndView loginQueryContent(ContentView view, Model model,
			HttpServletRequest request, HttpServletResponse response) {

		String contenttype = request.getParameter("ctype");
		if (!CheckEmpty.isEmpty(contenttype)) {
			view.setContentType(Long.parseLong(contenttype));
		}
		view.setIsRelease(Long.parseLong(Constants.CONTENT_ISRELEASE_YES));
		String page = DisplayGetPage.getPageParamName("contents", request);// 第几页
		if (page == null) {
			view.setBegin(0);
			view.setPageSize(Constants.pagesize);
		} else {
			view.setBegin((Integer.parseInt(page) - 1) * Constants.pagesize);
			view.setPageSize(Constants.pagesize);
		}
		String language = null;
		List list = null;
		if (!CheckEmpty.isEmpty(view.getLanguage())) {
			if (view.getLanguage().equals(
					PropertiesUtil.getPropertiesValue(
							"content.language.list.no", request)[0])) {
				language = Constants.TABLE_TYPE_CONTENT_ZH;
			} else if (view.getLanguage().equals(
					PropertiesUtil.getPropertiesValue(
							"content.language.list.no", request)[1])) {
				language = Constants.TABLE_TYPE_CONTENT_EN;
			} else {
				language = Constants.TABLE_TYPE_CONTENT_ZH;
			}
		} else {
			language = Constants.TABLE_TYPE_CONTENT_ZH;
		}
		view.setLanguage(language);
		list = conMan.getContentByType(view, language);
		int countsize = conMan.getContentNum(view);
		List<ContentView> listvo = null;
		if (null != list && list.size() > 0) {
			listvo = new ArrayList<ContentView>();
			for (int i = 0; i < list.size(); i++) {
				Content con = null;
				ContentEn conen = null;
				ContentView convo = null;
				if (language.equals(Constants.TABLE_TYPE_CONTENT_EN)) {
					conen = (ContentEn) list.get(i);
				} else {
					con = (Content) list.get(i);
				}
				convo = ContentCommon.toVo(con, conen);
				if (convo.getIsRelease().toString().equals(
						PropertiesUtil.getPropertiesValue(
								"content.isrelease.list.no", request)[1])) {
					convo.setStrIsRelease(PropertiesUtil.getPropertiesValue(
							"content.isrelease.list", request)[1]);
				} else {
					convo.setStrIsRelease(PropertiesUtil.getPropertiesValue(
							"content.isrelease.list", request)[2]);
				}
				for (int j = 0; j < PropertiesUtil.getPropertiesValue(
						"content.type.list", request).length; j++) {
					if (convo.getContentType().toString()
							.equals(
									PropertiesUtil.getPropertiesValue(
											"content.type.search.list.no",
											request)[j + 1])) {
						convo.setStrContentType(PropertiesUtil
								.getPropertiesValue("content.type.list",
										request)[j]);
					}
				}

				listvo.add(convo);
			}
		}
		// 内容类型
		String[] contenttypes = PropertiesUtil.getPropertiesValue("content.type.search.list",
				request);
		List<Item> contenttypelist = ReportComments.getList(contenttypes);
		model.addAttribute("contenttypelist", contenttypelist);
		// 语言类型
		String[] langagetype = PropertiesUtil.getPropertiesValue("content.language.list",
				request);
		List<Item> langagetypelist = ReportComments.getList(langagetype);
		model.addAttribute("langagetypelist", langagetypelist);
		model.addAttribute("listresult", true);
		model.addAttribute("contentsid", view);
		model.addAttribute("resultSize", countsize);
		model.addAttribute("pagesize", Constants.pagesize);
		double pagesize = Constants.pagesize;
		model.addAttribute("displayallpage", Math.ceil(countsize / pagesize));
		model.addAttribute("thispage", Integer.parseInt(page));
		return new ModelAndView("content/indexcontentlist", "contentlist",
				listvo);
	}

	/***
	 * 根据id查询内容详情
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unused")
	@RequestMapping("/content/contentinfo.html")
	private String getContentInfo(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		ContentView vo = null;
		String conid = request.getParameter("conid");
		String langType = request.getParameter("langtype");
		if (!CheckEmpty.isEmpty(conid)) {
			vo = conMan.getContentById(Long.parseLong(conid), langType);
		}
		if (null != vo) {
			model.addAttribute("coninfo", vo);
		}
		return "content/contentinfo";
	}

	/**
	 * 根据id删除内容
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/content/deletecontent.html")
	private String delcontent(HttpServletRequest request,
			HttpServletResponse response) {
		String conid = request.getParameter("conid");
		String language = request.getParameter("langtype");
		if (!CheckEmpty.isEmpty(conid) && !CheckEmpty.isEmpty(language)) {
			conMan.removeContentEn(Long.parseLong(conid), language);
		}
		String pagename = request.getParameter("pagename");
		String page = request.getParameter("thispage");
		if (!CheckEmpty.isEmpty(pagename) && !CheckEmpty.isEmpty(page)) {
			return "redirect:/content/querycontent.html?returntype=false&"
					+ pagename + "=" + page;
		} else {
			return "redirect:/content/querycontent.html";
		}
	}

	/**
	 * 根据id修改内容
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/content/updatecontent.html")
	private String updateContent(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		String conid = request.getParameter("conid");
		String language = request.getParameter("langtype");
		if (!CheckEmpty.isEmpty(conid) && !CheckEmpty.isEmpty(language)) {
			ContentView vo = conMan.getContentById(Long.parseLong(conid),
					language);
			vo.setContent(htmlspecialchars(vo.getContent()));
			model.addAttribute("contentinfo", vo);
		}
		String thispage = request.getParameter("thispage");
		String pagename = request.getParameter("pagename");
		model.addAttribute("thispage", thispage);
		model.addAttribute("pagename", pagename);
		return "content/addcontent";
	}

}
